package Teacher.Examination;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import Database.CallDB;
import Database.CourseMgmt;
import Database.ExaminationMgmt;
import Database.MyTableModel;
import Database.StudentMgmt;
import Exception.CheckDataException;
import Exception.ComboModelException;
import Exception.FormatScrollPaneException;
import Exception.InsertionDataException;
import Exception.Layer8DataException;
import Exception.LoadDriverException;
import Exception.ModificationDBException;
import General.CreaModPanel;
import General.GeneralInformation;
import General.GeneralMethod;
import General.GraphicsSetting;
import General.TreatError;
import GenericMethod.ComboBoxMethod;

public class PanelCreateModExamination extends CreaModPanel
{
	private ArrayList<String> arrayListGroup = new ArrayList<String>();
	private ArrayList<Integer> arrayListYear = new ArrayList<Integer>();
	private ArrayList<String> arrayListSection = new ArrayList<String>();

	private ComboBoxMethod comboMethod;
	private String[] tabType = {"January", "June", "September"};
	private JLabel labelCourse, labelGroup, labelType;
	private JComboBox<String> comboType, comboSection, comboGroup;
	private JComboBox<Object> comboCourse;
	private JComboBox<Integer> comboYear;
	private boolean modifyFlag = false;
	private JButton buttonValidate;
	public PanelCreateModExamination(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException, ComboModelException, LoadDriverException
	{
		super(callDB, connection);
		modifyFlag = false;
		
		creationComponent();
		showComponent();
	}
	
	public PanelCreateModExamination(CallDB callDB, Connection connection, String fieldName) throws CheckDataException, FormatScrollPaneException, ComboModelException, LoadDriverException
	{
		super(callDB, connection);
		modifyFlag = true;
		this.fieldName.setText(fieldName);
		
		creationComponent();
		showComponent();
	}
	
	public void setExamination() throws CheckDataException, NumberFormatException, InsertionDataException, ModificationDBException, LoadDriverException
	{
		MyTableModel idClass = new ExaminationMgmt(connection).selectIdClass(Integer.parseInt(comboYear.getSelectedItem().toString()), comboSection.getSelectedItem().toString(), comboGroup.getSelectedItem().toString());
		int idCourse = (int)((GeneralInformation.getCourseLeftModel().getValueAt(comboCourse.getSelectedIndex(), 1)));
		
		if(modifyFlag == false)
			new ExaminationMgmt(connection).createElement(fieldName.getText(), comboType.getSelectedItem().toString(), idCourse, (int)idClass.getValueAt(0, 0));
		else
			new ExaminationMgmt(connection).modifyElement((int)GeneralInformation.getIdSelectedLeftTable(), fieldName.getText(), comboType.getSelectedItem().toString(), idCourse, (int)idClass.getValueAt(0, 0));
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonDone)
					done();
				
				else if(arg0.getSource() == comboType)
					changeArray();
				
				else if(arg0.getSource() == comboCourse)
					clearAndStrictFillArray();
				
				else if(arg0.getSource() == buttonValidate)
					groupsecyear();
			}
			catch(CheckDataException | NumberFormatException | InsertionDataException | ModificationDBException | Layer8DataException | LoadDriverException | FormatScrollPaneException error)
			{
				TreatError.showError(error);
			}
		}
	}
	
	public void done() throws CheckDataException, LoadDriverException, NumberFormatException, InsertionDataException, ModificationDBException, Layer8DataException
	{
		boolean differentFieldName = new ExaminationMgmt(connection).checkFieldName(fieldName.getText());
		
		if(fieldName.getText().equals("") != true && date.getDate() != null)			
			if(differentFieldName == true)
				setExamination();
			else
				throw new Layer8DataException("This name is already used !");
		else
			throw new Layer8DataException();
	}
	
	public void changeArray() throws CheckDataException, LoadDriverException
	{				
		if(comboType.getSelectedIndex() == 2) //If the type is second sess
			clearAndFullFillArray();
		else
			clearAndStrictFillArray();
	}
	
	public void groupsecyear() throws FormatScrollPaneException, NumberFormatException, CheckDataException, LoadDriverException
	{
		myRightTableModel = new StudentMgmt(connection).selectSomeElements(Integer.parseInt(comboYear.getSelectedItem().toString()), comboSection.getSelectedItem().toString(), comboGroup.getSelectedItem().toString());
		refreshSuperRightTable();
	}
	
	public void clearAndFullFillArray()
	{
		arrayListYear.clear();
		arrayListSection.clear();
		arrayListGroup.clear();
		
		GeneralMethod.arrayFill(arrayListGroup, arrayListYear, arrayListSection);
		
		refreshCombo();
	}
	
	public void clearAndStrictFillArray() throws CheckDataException, LoadDriverException
	{
		MyTableModel tempId = new CourseMgmt(connection).selectIdLibelle(comboCourse.getSelectedItem().toString()); //Get id from the comboCourse
		MyTableModel tableYearSection = new CourseMgmt(connection).selectYearSec((int)tempId.getValueAt(0, 0)); //Get link year, section from idCourse
		MyTableModel tableGroup = new CourseMgmt(connection).selectBoundClasses((int)tempId.getValueAt(0, 0)); //Get link group from idCourse

		arrayListYear.clear();
		arrayListSection.clear();
		arrayListGroup.clear();
		
		for(int cpt = 0; cpt < tableYearSection.getRowCount(); cpt++){
			arrayListYear.add((int)tableYearSection.getValueAt(cpt, 0));
			arrayListSection.add((String)tableYearSection.getValueAt(cpt, 1));}
		
		for(int cpt = 0; cpt < tableGroup.getRowCount(); cpt++)
			arrayListGroup.add((String)tableGroup.getValueAt(cpt, 0));
		
		refreshCombo();
	}
	
	public void refreshCombo()
	{
		comboYear.removeAllItems();
		comboSection.removeAllItems();
		comboGroup.removeAllItems();
		
		comboMethod.comboArrayInteger(comboYear, arrayListYear);
		comboMethod.comboArrayString(comboSection, arrayListSection);
		comboMethod.comboArrayString(comboGroup, arrayListGroup);
	}
	
	public void comboCreation(MyActionListener actionListener) throws CheckDataException, ComboModelException, LoadDriverException
	{
		comboMethod.comboModel(comboCourse, GeneralInformation.getCourseLeftModel(), "column", 0);

		if(modifyFlag == true)
			clearAndStrictFillArray();
		else
			clearAndFullFillArray();
		
		for(String item : tabType)
			comboType.addItem(item);

		comboGroup.addActionListener(actionListener);
		comboSection.addActionListener(actionListener);
		comboYear.addActionListener(actionListener);
		comboType.addActionListener(actionListener);
		comboCourse.addActionListener(actionListener);
	}
	
	public void creationComponent() throws CheckDataException, ComboModelException, LoadDriverException
	{
		MyActionListener actionListener = new MyActionListener();
		comboMethod = new ComboBoxMethod();
		
		comboSection = new JComboBox<String>();
		comboYear = new JComboBox<Integer>();
		comboGroup = new JComboBox<String>();
		comboType = new JComboBox<String>();
		comboCourse = new JComboBox<Object>();

		comboCreation(actionListener);

		buttonValidate = new JButton("Validate");
		
		labelType = new JLabel("Type");
		labelType.setHorizontalAlignment(SwingConstants.LEFT);
		
		labelCourse = new JLabel("Course");
		labelCourse.setHorizontalAlignment(SwingConstants.LEFT);
		
		labelGroup = new JLabel("Group");
		labelGroup.setHorizontalAlignment(SwingConstants.LEFT);
		
		buttonDone.addActionListener(actionListener);
		buttonBack.addActionListener(actionListener);
		buttonValidate.addActionListener(actionListener);
	}
	
	public void showComponent() throws FormatScrollPaneException
	{	
		labelCourse.setBounds(40, 120, 55, 30);	comboCourse.setBounds(115, 120, 175, 30);
		labelGroup.setBounds(40, 190, 55, 30); comboYear.setBounds(115, 190, 55, 30); comboSection.setBounds(175, 190, 55, 30); comboGroup.setBounds(235, 190, 55, 30); buttonValidate.setBounds(295, 190, 75, 30);
		labelType.setBounds(40, 330, 55, 30); comboType.setBounds(115, 330, 175, 30);
		labelDate.setBounds(40, 260, 55, 30); date.setBounds(115, 260, 175, 30);
		
		this.add(labelCourse); this.add(comboCourse);
		this.add(labelGroup); this.add(comboYear); this.add(comboSection); this.add(comboGroup); this.add(buttonValidate);
		this.add(labelType); this.add(comboType);
		this.add(labelDate); this.add(date);

		GraphicsSetting.setBackground(this, "image/main.png", 0, 0, 800, 600);
	}
}